import axios from 'axios';
import path from './path.js'
import {ElMessage} from 'element-plus'
import useRouter from '@/router/index.js'
import {userDefineStore} from "../Pinia/stores/user.js"

//网络请求公共配置
let axiosInstance = axios.create({
    baseURL: path.baseUrl,
    //最大接收时间
    timeout: 3600,
});

// 添加请求拦截器
axiosInstance.interceptors.request.use(config => {
    let counterStore = userDefineStore();

    try {
        let token = counterStore.userInfo.token;
        if (token) {
            // 将 token 添加到 Authorization 头中
            config.headers['Authorization'] = `Bearer ${token}`;
            // config.headers.set('Bearer ', token);
        }
    } catch (e) {
    }


    // 在发送请求之前做些什么
    return config;
}, error => {

    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axiosInstance.interceptors.response.use(
    response => response.data
    ,
    error => {
        ElMessage({
            type: "error",
            message:  error.response.data.msg
        })
        return Promise.reject(error);
    }
)


export default axiosInstance;
